﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Mail;
using System.Web.UI;

namespace PayPalSample1
{
    public partial class IPN : Page
    {
        //***Code is incomplete, likely does not work
        protected void Page_Load(object sender, EventArgs e)
        {
            //Read in IPN POST 
            String strFormValues = Encoding.ASCII.GetString(Request.BinaryRead(Request.ContentLength));
            string strNewValue = "";

            //Create the request back
            var req = (HttpWebRequest) WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");


            //' Set values for the request back
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            strNewValue = strFormValues + "&cmd=_notify-validate";
            req.ContentLength = strNewValue.Length;

            // Write the request back IPN strings
            var stOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
            stOut.Write(strNewValue);
            stOut.Close();


            //send the request, read the response

            var strResponse = (HttpWebResponse) req.GetResponse();

            Stream IPNResponseStream = strResponse.GetResponseStream();
            Encoding encode = Encoding.GetEncoding("utf-8");
            var readStream = new StreamReader(IPNResponseStream, encode);


            var read = new char[256];

            // Reads 256 characters at a time.
            int count = readStream.Read(read, 0, 256);

            while (count > 0)
            {
                // Dumps the 256 characters to a string and displays the string to the console.
                //string IPNResponse;
                //IPNResponse.IndexOf(read, 0, count);

                ////Dim IPNResponse As New [String](read, 0, count)
                //count = readStream.Read(read, 0, 256);

                ////if IPN response was VERIFIED..perform VERIFIED handling
                ////for this example - send email of raw IPN string

                //if (IPNResponse == "VERIFIED")
                //{
                //    var myEmail = new MailMessage();
                //    myEmail.To = "poneal@ebay.com";
                //    myEmail.From = "webmaster@oma-ets-04.corp.ebay.com";
                //    myEmail.Body = strNewValue;
                //    myEmail.Subject = IPNResponse & " IPN";
                //    SmtpMail.SmtpServer = "localhost";
                //    SmtpMail.Send(myEmail);
                //}
                //{
                //    // if IPN is INVALID
                //    //log and manually investigate

                //    MailMessage myEmail;
                //    myEmail = new MailMessage();
                //    myEmail.To = "poneal@ebay.com";
                //    myEmail.From = "webmaster@oma-ets-04.corp.ebay.com";
                //    myEmail.Body = strNewValue;
                //    myEmail.Subject = IPNResponse & " IPN";
                //    SmtpMail.SmtpServer = "localhost";
                //    SmtpMail.Send(myEmail);
                }
            }

            //tidy up, close streams
            //readStream.Close();
            //strResponse.Close();


            //for debug
            //Response.Write("Script ran, check your email")
        }
    }
